<template>
  <div>
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="数据名称" prop="dataname">
        <el-input v-model="dataForm.dataname" placeholder="" />
      </el-form-item>
      <el-form-item label="数据值" prop="dataval">
        <el-input v-model="dataForm.dataval" placeholder="" />
      </el-form-item>
      <el-form-item label="排序号" prop="datasort">
        <el-input v-model="dataForm.datasort" placeholder="" />
      </el-form-item>
    </el-form>
    <el-row>
      <el-col style="text-align: right;display: block;">
        <el-button size="mini" icon="el-icon-check" type="primary" @click="dataFormSubmit()">确定
        </el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        catid: '',
        dataname: '',
        dataval: '',
        datasort: '',
        dataflag: ''
      },
      dataRule: {
        catid: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        dataname: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        dataval: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        datasort: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        dataflag: [
          { required: true, message: '数据有效标志1:有效 -1:无效不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id, catid) {
      this.dataForm.catid = catid
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/data/infodatas/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.catid = data.datas.catid
              this.dataForm.dataname = data.datas.dataname
              this.dataForm.dataval = data.datas.dataval
              this.dataForm.datasort = data.datas.datasort
              this.dataForm.dataflag = data.datas.dataflag
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/data/${!this.dataForm.id ? 'savedatas' : 'updatedatas'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'catid': this.dataForm.catid,
              'dataname': this.dataForm.dataname,
              'dataval': this.dataForm.dataval,
              'datasort': this.dataForm.datasort,
              'dataflag': this.dataForm.dataflag
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  debugger
                  this.visible = false
                  this.$emit('refreshDataList', this.dataForm.catid)
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
